# This file is used to create a login blueprint for the flask application.
# The blueprint is used to handle the login functionality of the application.


from flask import Blueprint
from flask import request
from flask import jsonify
import psycopg2.extras
from settings import *
from database import PostgreSingleton
from database.wx_manager_authentication_methods import WxManagerAuthenticationMethods
from database.wx_organization_authentication_methods import WxOrganizationAuthenticationMethods
from database.wx_expert_authentication import WxExpertAuthenticationDaoMethods
from database.wx_authentication_aggregation_methods import WxAuthenticationAggregationMethods
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

wx_authentication_blueprint = Blueprint('wx_authentication_blueprint', __name__)

# 传家认证相关接口

@wx_authentication_blueprint.route('/get_wx_expert_authentication_by_wx_id', methods=['GET'])
def get_wx_expert_authentication_by_wx_id():

    wx_id = request.args.get('wx_id')

    if not wx_id:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }

        return jsonify(args_error)

    
    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_expert_authentication_dao = WxExpertAuthenticationDaoMethods(conn, cur)
        result = wx_expert_authentication_dao.get_wx_expert_authentication_by_wx_id(wx_id)

        if not result:
            data_error = {
                "status": -1,
                "errorCode": 1002,
                "errorMsg": "数据错误",
                "resultBody": None
            }

            return jsonify(data_error)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": result
        }

        return jsonify(result_body)


@wx_authentication_blueprint.route('/insert_or_update_wx_expert_authentication', methods=['POST'])
def insert_or_update_wx_expert_authentication():

    wx_id = request.form.get('wx_id')
    # sex = request.form.get('sex')
    education = request.form.get('education') # 学历
    job_title = request.form.get('job_title') # 职称/专业技术职务
    specialty = request.form.get('specialty') # 专家类别
    job = request.form.get('job') # 职务
    personal_introduction = request.form.get('personal_introduction') # 专家简介
    # authentication_status = request.form.get('authentication_status')
    # authentication_time = request.form.get('authentication_time')

    if not wx_id or not education or not job_title or not job or not specialty or not personal_introduction:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_expert_authentication_dao = WxExpertAuthenticationDaoMethods(conn, cur)
        wx_expert_authentication_dao.insert_or_update_wx_expert_authentication(wx_id, education, job_title, specialty, job, personal_introduction)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": "success"
        }

        return jsonify(result_body)


@wx_authentication_blueprint.route('/update_wx_expert_authentication_status', methods=['POST'])
def update_wx_expert_authentication_status():

    wx_id = request.form.get('wx_id')
    authentication_status = request.form.get('authentication_status')
    # authentication_time = request.form.get('authentication_time')

    if not wx_id or not authentication_status:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_expert_authentication_dao = WxExpertAuthenticationDaoMethods(conn, cur)
        wx_expert_authentication_dao.update_authentication_status(wx_id, authentication_status)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg":  None,
            "resultBody": "success"
        }

        return jsonify(result_body)
    
# # 获取所有未认证的专家


@wx_authentication_blueprint.route('/get_all_un_authentication_expert', methods=['GET'])
def get_all_un_authentication_expert():
    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_expert_authentication_dao = WxExpertAuthenticationDaoMethods(conn, cur)
        result = wx_expert_authentication_dao.get_all_un_authentication_expert(page, limit)
        total = wx_expert_authentication_dao.get_all_un_authentication_expert_count()

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total
            }
        }

        return jsonify(result_body)


@wx_authentication_blueprint.route('/get_all_authentication_expert', methods=['GET'])
def get_all_authentication_expert():
    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_expert_authentication_dao = WxExpertAuthenticationDaoMethods(conn, cur)
        result = wx_expert_authentication_dao.get_all_authentication_expert(page, limit)
        total = wx_expert_authentication_dao.get_all_authentication_expert_count()

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total
            }
        }
        

        return jsonify(result_body)


@wx_authentication_blueprint.route('/get_all_expert', methods=['GET'])
def get_all_expert():
    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_expert_authentication_dao = WxExpertAuthenticationDaoMethods(conn, cur)
        result = wx_expert_authentication_dao.get_all_expert(page, limit)
        total = wx_expert_authentication_dao.get_all_expert_count()

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total
            }
        }

        return jsonify(result_body)
    


# 管理者认证相关接口(技术经理人认证接口)


@wx_authentication_blueprint.route('/get_manager_authentication_by_wx_id', methods=['GET'])
def get_manager_authentication_by_wx_id():

    wx_id = request.args.get('wx_id')

    if not wx_id:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }   
        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_manager_authentication_methods = WxManagerAuthenticationMethods(conn, cur)
        result = wx_manager_authentication_methods.get_manager_authentication_by_wx_id(wx_id)

        if not result:
            data_error = {
                "status": -1,
                "errorCode": 1002,
                "errorMsg": "数据错误",
                "resultBody": None
            }

            return jsonify(data_error)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": result
        }

        return jsonify(result_body)


@wx_authentication_blueprint.route('/insert_or_update_manager_authentication', methods=['POST'])
def insert_or_update_manager_authentication():

    wx_id = request.form.get('wx_id')
    job_title = request.form.get('job_title', default='无')
    awards = request.form.get('awards', default='无')
    social_job = request.form.get('social_job', default='无')
    # 证件照片 链接
    certificate_photo = request.form.get('certificate_photo')
    # 证书级别 初 中 高
    certificate_level = request.form.get('certificate_level')

    
    params = {
        "wx_id": wx_id,
        "awards": awards,
        "job_title": job_title,
        "social_job": social_job,
        "certificate_photo": certificate_photo,
        "certificate_level": certificate_level
    }

    missing_params = [name for name, value in params.items() if not value]

    if missing_params:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误: 缺失的参数为: " + ", ".join(missing_params),
            "resultBody": None
        }

        return jsonify(args_error)
    
    if certificate_level not in ['初级', '中级', '高级']:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误: 证书级别只能为初级、中级、高级", # 这里的错误信息需要修改
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) # cursor for executing queries
        wx_manager_authentication_methods = WxManagerAuthenticationMethods(conn, cur)
        # wx_manager_authentication_methods.insert_or_update_manager_authentication(wx_id, real_name, phone_number, sex, birth_date, unit_name, unit_property, unit_address, academic_background, job, job_title, certificate, certificate_level, awards, social_job, personal_introduction, personal_photo, personal_resume, certificate_photo)
        wx_manager_authentication_methods.insert_or_update_manager_authentication(wx_id, awards, job_title, social_job, certificate_photo, certificate_level)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": None
        }

        return jsonify(result_body)
    
@wx_authentication_blueprint.route('/update_manager_authentication_status', methods=['POST'])
def update_manager_authentication_status():

    wx_id = request.form.get('wx_id')
    authentication_status = request.form.get('authentication_status')
    # authentication_time = request.form.get('authentication_time')

    if not wx_id or not authentication_status:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_manager_authentication_methods = WxManagerAuthenticationMethods(conn, cur)
        wx_manager_authentication_methods.update_authentication_status(wx_id, authentication_status)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": None
        }

        return jsonify(result_body)
    

@wx_authentication_blueprint.route('/get_all_manager_authentication', methods=['GET'])
def get_all_manager_authentication():

    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_manager_authentication_methods = WxManagerAuthenticationMethods(conn, cur)
        result = wx_manager_authentication_methods.get_all_manager_authentication(page, limit)
        total = wx_manager_authentication_methods.get_all_manager_authentication_count()

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total                 
            }
        }

        return jsonify(result_body)

@wx_authentication_blueprint.route('/get_authentication_status_true', methods=['GET'])
def get_authentication_status_true():

    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_manager_authentication_methods = WxManagerAuthenticationMethods(conn, cur)
        result = wx_manager_authentication_methods.get_authentication_status_true(page, limit)
        total = wx_manager_authentication_methods.get_authentication_status_true_count()


        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total                 
            }
        }

        return jsonify(result_body)

@wx_authentication_blueprint.route('/get_authentication_status_false', methods=['GET'])
def get_authentication_status_false():

    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_manager_authentication_methods = WxManagerAuthenticationMethods(conn, cur)
        result = wx_manager_authentication_methods.get_authentication_status_false(page, limit)
        total = wx_manager_authentication_methods.get_authentication_status_false_count()


        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total                 
            }
        }

        return jsonify(result_body)




    # 企业认证相关接口(企业认证接口)


@wx_authentication_blueprint.route('/get_wx_organization_authentication_by_wx_id', methods=['GET'])
def get_wx_organization_authentication_by_wx_id():

    wx_id = request.args.get('wx_id')

    if not wx_id:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_organization_authentication_methods = WxOrganizationAuthenticationMethods(conn, cur)
        result = wx_organization_authentication_methods.get_wx_organization_authentication_by_wx_id(wx_id)


        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": result
        }

        return jsonify(result_body)


@wx_authentication_blueprint.route('/insert_or_update_wx_organization_authentication', methods=['POST'])
def insert_or_update_wx_organization_authentication():

    wx_id = request.form.get('wx_id')
    organization_name = request.form.get('organization_name') # 组织名称
    organization_type = request.form.get('organization_type')
    organization_direction = request.form.get('organization_direction')
    area = request.form.get('area')
    address = request.form.get('address')
    introduction = request.form.get('introduction')
    main_business = request.form.get('main_business')
    contact_name = request.form.get('contact_name') # 联系人姓名
    contact_number = request.form.get('contact_number') # 联系人电话
    contact_job = request.form.get('contact_job', default='无')
    authentication_status = request.form.get('authentication_status')
    # authentication_time = request.form.get('authentication_time')
    business_license_photo=request.form.get('business_license_photo')

    # 将所有参数和对应的名称放入字典中
    params = {
        "wx_id": wx_id,
        "organization_name": organization_name,
        "organization_type": organization_type,
        "organization_direction": organization_direction,
        "area": area,
        "address": address,
        "introduction": introduction,
        "main_business": main_business,
        "contact_name": contact_name,
        "contact_number": contact_number,
        "contact_job": contact_job,
        "business_license_photo": business_license_photo,
        # "authentication_status": authentication_status,
        # "authentication_time": authentication_time
    }

    # 检查缺失的参数
    missing_params = [name for name, value in params.items() if not value]

    if missing_params:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误: 缺失的参数为: " + ", ".join(missing_params),
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_organization_authentication_methods = WxOrganizationAuthenticationMethods(conn, cur)
        wx_organization_authentication_methods.insert_or_update_wx_organization_authentication(wx_id,organization_name, organization_direction, area, address, introduction, main_business, contact_name, contact_number, contact_job, business_license_photo)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": None
        }

        return jsonify(result_body)
    
@wx_authentication_blueprint.route('/update_wx_organization_authentication_status', methods=['POST'])
def update_wx_organization_authentication_status():

    wx_id = request.form.get('wx_id')
    authentication_status = request.form.get('authentication_status')
    # authentication_time = request.form.get('authentication_time')

    if not wx_id or not authentication_status:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_organization_authentication_methods = WxOrganizationAuthenticationMethods(conn, cur)
        wx_organization_authentication_methods.update_authentication_status(wx_id, authentication_status)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": "success"
        }

        return jsonify(result_body)
    

@wx_authentication_blueprint.route('/get_all_wx_organization_authentication', methods=['GET'])
def get_all_wx_organization_authentication():

    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_organization_authentication_methods = WxOrganizationAuthenticationMethods(conn, cur)
        result = wx_organization_authentication_methods.get_all_wx_organization_authentication(page, limit)
        total = wx_organization_authentication_methods.get_count_wx_organization_authentication()

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total                 
            }
        }

        return jsonify(result_body)

@wx_authentication_blueprint.route('/get_authentication_status_true_wx_organization', methods=['GET'])
def get_authentication_status_true_wx_organization():

    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_organization_authentication_methods = WxOrganizationAuthenticationMethods(conn, cur)
        result = wx_organization_authentication_methods.get_all_authentication_status_true(page, limit)
        total = wx_organization_authentication_methods.get_count_authentication_status_true()

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total                 
            }
        }

        return jsonify(result_body)

@wx_authentication_blueprint.route('/get_authentication_status_false_wx_organization', methods=['GET'])
def get_authentication_status_false_wx_organization():

    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=10)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_organization_authentication_methods = WxOrganizationAuthenticationMethods(conn, cur)
        result = wx_organization_authentication_methods.get_all_authentication_status_false(page, limit)
        total = wx_organization_authentication_methods.get_count_authentication_status_false()

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": {
                "list": result,
                "total": total                 
            }
        }

        return jsonify(result_body)
    

@wx_authentication_blueprint.route('/wx_authentication_aggregation', methods=['GET'])
def wx_authentication_aggregation():

    wx_id = request.args.get('wx_id')

    if not wx_id:
        args_error = {
            "status": -1,
            "errorCode": 1001,
            "errorMsg": "参数错误",
            "resultBody": None
        }

        return jsonify(args_error)

    with PostgreSingleton() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        wx_authentication_aggregation_methods = WxAuthenticationAggregationMethods(conn, cur)

        result = wx_authentication_aggregation_methods.get_wx_authentication_aggregation(wx_id)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": "success",
            "resultBody": result
        }

        return jsonify(result_body)